// page/bySingle/bySingle.js
const http = require('../../../utils/http')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    res1: null, //1未结算总页
    res2: null, //2制单总页
    current: 1, //tab切换
    perpage: 10, //条数
    page1: 1, //1未结算分页
    page2: 1, //2制单分页
    itema: [], //2制单
    itemb: [] //1未结算
  },
  tab(e) {
    if(e.currentTarget.dataset.tab == 1) {
      this.cregetlist1()
    } else {
      this.cregetlist2()
    }
    this.setData({
      current: e.currentTarget.dataset.tab
    })
  },
  newCard() { //新建卡单页面跳转
    wx.navigateTo({
      url: '/pages/openCard/openCard/openCard',
    })
  },
  jumpCard(e) { //详情页跳转
    // console.log(e.currentTarget.dataset.id)
    if (this.data.current == 2) { //去展示页面
      let id = e.currentTarget.dataset.id.id
      let docnumber = e.currentTarget.dataset.id.document_number
      wx.navigateTo({
        url: `/pages/openCard/cardDetailshuw/cardDetailshow?id=${id}&docnumber=${docnumber}`,
        // url: `/pages/openCard/cardDetailshuw/cardDetailshuw`,

        //url: '/pages/openCard/cardDetail/cardDetail?id=' + id+'docnumber='+docnumber,
      })
    } else { //去修改页面
      let id = e.currentTarget.dataset.id.id
      let docnumber = e.currentTarget.dataset.id.document_number
      wx.navigateTo({
        url: `/pages/openCard/cardDetail/cardDetail?id=${id}&docnumber=${docnumber}`,
        //url: '/pages/openCard/cardDetail/cardDetail?id=' + id+'docnumber='+docnumber,
      })
    }

  },
  bindinput(e) {
    http.postRequest('/mini/v1/vip/document_list', {
      search: e.detail.value, //搜索内容
      document_status: this.data.current,
      per_page: 10,
      page: 1
    }, res => {
      let {
        data
      } = res.data
      if (this.data.current == 2) {
        this.setData({
          itema: data
        })
      } else {
        this.setData({
          itemb: data
        })
      }
    })
  },
  cregetlist2() { //2：制单
    http.postRequest('/mini/v1/vip/document_list', {
      document_status: 2,
      per_page: 10,
      page: 1
    }, res => {
      if (res.msg == 'ok') {
        let {
          data
        } = res.data
        this.setData({
          itema: data,
          page2: this.data.page2 + 1,
          res2: res.data.last_page,
        })
      } else {
        wx: showToast({
          title: res.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  cregetlist1() { //1:未结算
    http.postRequest('/mini/v1/vip/document_list', {
      document_status: 1,
      per_page: 10,
      page: 1
    }, res => {
      if (res.msg == 'ok') {
        let {
          data
        } = res.data
        this.setData({
          itemb: data,
          page1: this.data.page1 + 1,
          res1: res.data.last_page,
        })
      } else {
        wx: showToast({
          title: res.msg,
          icon: 'none',
          duration: 1500
        })
      }
    })
  },
  newgetlist(page) {
    //只用于上拉加载
    http.postRequest('/mini/v1/vip/document_list', {
      document_status: this.data.current,
      per_page: 10,
      page: page
    }, res => {
      if (res.msg == 'ok') {
        console.log(res)
        let {
          data
        } = res.data
        if (this.data.current == 1) {
          let newarr = this.data.itemb
          newarr.push(...data)
          this.setData({
            itemb: newarr,
            page1: this.data.page1 + 1,
            // res1: res.data.last_page,
          })
        } else {
          let newarr = this.data.itemb
          newarr.push(...data)
          this.setData({
            itema: newarr,
            page2: this.data.page2 + 1,
            // res2: res.data.last_page
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },
  onShow() {
    if(this.data.current == 1) {
      this.cregetlist1()
    } else {
      this.cregetlist2()
    }
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    console.log('已触发')
    if (this.data.current == 2) {
      this.cregetlist2()
      wx.stopPullDownRefresh() //结束刷新
    } else {
      this.cregetlist1()
      wx.stopPullDownRefresh() //结束刷新
    }
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.current == 2 && this.data.page2 <= this.data.res2) {
      this.newgetlist(this.data.page2)
    } else if (this.data.current == 1 && this.data.page1 <= this.data.res1) {
      this.newgetlist(this.data.page1)
    } else {
      wx.showToast({
        title: '我是有底线的',
        icon: 'nome',
        duration: 1500
      })
    }
  }
})